package com.shengwei.truck.driver.utils;
import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import com.shengwei.truck.driver.bean.MyConstant;
import com.shengwei.truck.driver.receiver.AlarmHeartReceiver;
/**
 * 定时器
 */
public class AlarmManagerUtils {
    private  long HEART_INTERVAL = 4 * 60 * 1000;//闹钟执行任务的时间间隔
    private Context mContext;
    public static AlarmManager am;
    public static PendingIntent pendingIntent;
    String TAG = "AlarmManagerUtils";
    //
    private AlarmManagerUtils(Context aContext) {
        this.mContext = aContext;
    }

    //饿汉式单例设计模式
    private static AlarmManagerUtils instance = null;

    public static AlarmManagerUtils getInstance(Context aContext) {
        if (instance == null) {
            synchronized (AlarmManagerUtils.class) {
                if (instance == null) {
                    instance = new AlarmManagerUtils(aContext);
                }
            }
        }
        return instance;
    }

    public PendingIntent createBroadcastPendingIntent() {
        Intent intent = new Intent(MyConstant.ALARM_HEART_BROADCAST);
        intent.setClass(mContext, AlarmHeartReceiver.class); //从android8开始，如果要让在mainfest里注册的静态广播起效，则必须发送显示广播，即指定广播类名AlarmHeartReceiver.class
        pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);//每隔5秒发送一次广播
        return pendingIntent;
    }

    public void createGetUpAlarmManager() {
       if(am != null && pendingIntent != null)
        {
            am.cancel(pendingIntent);
        }
        am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        createBroadcastPendingIntent();
    }

    @SuppressLint("NewApi")
    public void getUpAlarmManagerStartWork() {
        //版本适配
        long alarmTime =  System.currentTimeMillis() + 1000;
        AlarmHeartReceiver.lastSendTime = alarmTime;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {// 6.0及以上
            AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(alarmTime,pendingIntent );
            am.setAlarmClock(alarmClockInfo,pendingIntent);
        }
        else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {// 4.4及以上
            am.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
        } else {
            am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, HEART_INTERVAL, pendingIntent);
        }
    }

    @SuppressLint("NewApi")
    public void getUpAlarmManagerWorkOnReceiver() {
        //高版本重复设置闹钟达到低版本中setRepeating相同效果
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {// 6.0及以上
            int brandId = CommonUtil.getMobileBrandId();

            switch (brandId) {
                case 1://huawei
                case 2: //xiaomi
                case 3: //OPPO
                    HEART_INTERVAL = 4 * 60 * 1000;
                    break;
                case 4:  //vivo 心跳间隔设置为1分钟
                    HEART_INTERVAL = 1 * 60 * 1000;
                    break;
                default:
                    HEART_INTERVAL = 4 * 60 * 1000;
                    break;
            }
           AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(System.currentTimeMillis() + HEART_INTERVAL,null );
           if(am == null) {
               am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
           }
           if(pendingIntent == null) {
               pendingIntent = createBroadcastPendingIntent();
           }
           am.setAlarmClock(alarmClockInfo,pendingIntent);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {// 4.4及以上
            am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + HEART_INTERVAL, pendingIntent);
        }
    }
}